﻿#include "Config.h"
#include <QTextCodec>
#include <QMessageBox>
#include <QPushButton>
#include <QCoreApplication>
using namespace std;
bool Config::m_sWriteAbled = false;
bool Config::CheckIniPath()
{
    QString strIniFile = QCoreApplication::applicationDirPath()+INIPATH;
    QFileInfo file(strIniFile);
    if(!file.isFile())
    {
        qDebug()<<"INI IS NOT EXIST";
        return false;
    }
    if(!m_sWriteAbled&&!file.isWritable())
    {
        QMessageBox *result= new QMessageBox(QMessageBox::Critical,"提示",
                                              "配置文件没有写权限",QMessageBox::Yes);
        (result->button(QMessageBox::Yes))->setText("确定");
        result->exec();
        delete result;
    }
    m_sWriteAbled = true;
    return true;
}

//bool Config::readUserData(QList<User> &list_user)
//{
//    if(!CheckIniPath())
//        return -1;
//    QString strIniFile = QCoreApplication::applicationDirPath()+INIPATH;
//    QSettings set(strIniFile,QSettings::IniFormat);
//    set.setIniCodec("UTF-8");
//    QString temp = section + "/"+key;
//    return set.value(temp).toInt();
//}

bool Config::readUrl(QString &url)
{
    if(!CheckIniPath())
        return false;
    url.clear();
    QString strIniFile = QCoreApplication::applicationDirPath()+INIPATH;
    QSettings set(strIniFile,QSettings::IniFormat);
    set.setIniCodec("UTF-8");
    QString temp =  "network/url";
    url = set.value(temp).toString();
    if(url.isEmpty())
        return false;
    return true;
}



int Config::GetConfigIntData(const QString& section, const QString& key)
{
    if(!CheckIniPath())
        return -1;
    QString strIniFile = QCoreApplication::applicationDirPath()+INIPATH;
    QSettings set(strIniFile,QSettings::IniFormat);
    set.setIniCodec("UTF-8");
    QString temp = section + "/"+key;
    return set.value(temp).toInt();
}

long long Config::GetConfigLongLongData(const QString &section, const QString &key)
{
    if(!CheckIniPath())
        return -1;
    QString strIniFile = QCoreApplication::applicationDirPath()+INIPATH;
    QSettings set(strIniFile,QSettings::IniFormat);
    set.setIniCodec("UTF-8");
    QString temp = section + "/"+key;
    return set.value(temp).toLongLong();
}

QString Config::GetConfigStringData(const QString& section, const QString& key)
{
    if(!CheckIniPath())
        return "";
    QString strIniFile = QCoreApplication::applicationDirPath()+INIPATH;
    QSettings set(strIniFile,QSettings::IniFormat);
    set.setIniCodec("UTF-8");
    //set.setIniCodec(QTextCodec::codecForName("utf-8"));
    QString temp = section + "/"+key;
    return set.value(temp).toString();
}

float Config::GetConfigFloatData(const QString &section, const QString &key)
{
    if(!CheckIniPath())
        return 0.0;
    QString strIniFile = QCoreApplication::applicationDirPath()+INIPATH;
    QSettings set(strIniFile,QSettings::IniFormat);
    set.setIniCodec("UTF-8");
    QString temp = section + "/"+key;
    return set.value(temp).toFloat();
}

void Config::WriteStringToConfig(const QString &section, const QString &key, const QString &value)
{
    if(!CheckIniPath())
        return ;
    QString strIniFile = QCoreApplication::applicationDirPath()+INIPATH;
    QSettings set(strIniFile,QSettings::IniFormat);
    set.setIniCodec("UTF-8");
    QString temp = section + "/"+key;
    set.setValue(temp,value);
    return;
}
